home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevpcx.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  293 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevpcx.c */
  20. /* PCX file format devices for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gdevpccm.h"
  23.  
  24. /* Thanks to Phil Conrad for donating the original version */
  25. /* of these drivers to Aladdin Enterprises. */
  26.  
  27. /* ------ The device descriptors ------ */
  28.  
  29. /*
  30.  * Default X and Y resolution.
  31.  */
  32. #define X_DPI 72
  33. #define Y_DPI 72
  34.  
  35. /* Monochrome. */
  36.  
  37. private dev_proc_print_page(pcxmono_print_page);
  38.  
  39. gx_device_printer far_data gs_pcxmono_device =
  40.   prn_device(prn_std_procs, "pcxmono",
  41.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  42.     X_DPI, Y_DPI,
  43.     0,0,0,0,            /* margins */
  44.     1, pcxmono_print_page);
  45.  
  46. /* 4-bit planar (EGA/VGA-style) color. */
  47.  
  48. private dev_proc_print_page(pcx16_print_page);
  49.  
  50. private gx_device_procs pcx16_procs =
  51.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  52.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  53. gx_device_printer far_data gs_pcx16_device =
  54.   prn_device(pcx16_procs, "pcx16",
  55.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  56.     X_DPI, Y_DPI,
  57.     0,0,0,0,            /* margins */
  58.     4, pcx16_print_page);
  59.  
  60. /* Chunky 8-bit (SuperVGA-style) color. */
  61. /* (Uses a fixed palette of 3,3,2 bits.) */
  62.  
  63. private dev_proc_print_page(pcx256_print_page);
  64.  
  65. private gx_device_procs pcx256_procs =
  66.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  67.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  68. gx_device_printer far_data gs_pcx256_device =
  69.   prn_device(pcx256_procs, "pcx256",
  70.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  71.     X_DPI, Y_DPI,
  72.     0,0,0,0,            /* margins */
  73.     8, pcx256_print_page);
  74.  
  75. /* ------ Private definitions ------ */
  76.  
  77. /* All two-byte quantities are stored LSB-first! */
  78. #if arch_is_big_endian
  79. #  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
  80. #else
  81. #  define assign_ushort(a,v) a = (v)
  82. #endif
  83.  
  84. typedef struct pcx_header_s {
  85.     byte     manuf;        /* always 0x0a */
  86.     byte    version;    /* version info = 0,2,3,5 */
  87.     byte    encoding;    /* 1=RLE */
  88.     byte    bpp;        /* bits per pixel */
  89.     ushort    x1;        /* picture dimensions */
  90.     ushort    y1;
  91.     ushort    x2;
  92.     ushort    y2;
  93.     ushort    hres;        /* horz. resolution */
  94.     ushort    vres;        /* vert. resolution */
  95.     byte    palette[16*3];    /* color palette */
  96.     byte    vmode;        /* video mode for graphics board */
  97.     byte    nplanes;    /* number of color planes */
  98.     ushort    bpl;        /* number of bytes per line (uncompresses) */
  99.     ushort    palinfo;    /* palette info 1=color, 2=grey */
  100.     ushort    shres;        /* scanner horz. resolution */
  101.     ushort    svres;        /* scanner vert. resolution */
  102.     byte    xtra[58];    /* fill out header to 128 bytes */
  103. } pcx_header;
  104.  
  105. /* 
  106. ** version info for PCX is as follows 
  107. **
  108. ** 0 == 2.5
  109. ** 2 == 2.8 w/palette info
  110. ** 3 == 2.8 without palette info
  111. ** 5 == 3.0
  112. **
  113. */
  114.  
  115. /* Forward declarations */
  116. private void pcx_write_rle(P3(const byte *, const byte *, FILE *));
  117. private int pcx_write_page(P4(gx_device_printer *, FILE *, pcx_header _ss *, int));
  118.  
  119. /* Write an "old" PCX page. */
  120. static const byte ega_palette[16*3] = {
  121.   0x00,0x00,0x00,  0x00,0x00,0xaa,  0x00,0xaa,0x00,  0x00,0xaa,0xaa,
  122.   0xaa,0x00,0x00,  0xaa,0x00,0xaa,  0xaa,0xaa,0x00,  0xaa,0xaa,0xaa,
  123.   0x55,0x55,0x55,  0x55,0x55,0xff,  0x55,0xff,0x55,  0x55,0xff,0xff,
  124.   0xff,0x55,0x55,  0xff,0x55,0xff,  0xff,0xff,0x55,  0xff,0xff,0xff
  125. };
  126. private int
  127. pcx16_print_page(gx_device_printer *pdev, FILE *file)
  128. {    pcx_header header;
  129.     header.version = 2;
  130.     header.bpp = 1;
  131.     header.nplanes = 4;
  132.     /* Fill the EGA palette appropriately. */
  133.     memcpy((byte *)header.palette, ega_palette, sizeof(ega_palette));
  134.     return pcx_write_page(pdev, file, &header, 1);
  135. }
  136.  
  137. /* Write a "new" PCX page. */
  138. private int
  139. pcx256_print_page(gx_device_printer *pdev, FILE *file)
  140. {    pcx_header header;
  141.     int code;
  142.     header.version = 5;
  143.     header.bpp = 8;
  144.     header.nplanes = 1;
  145.     /* Clear the EGA palette */
  146.     memset((byte *)header.palette, 0, sizeof(header.palette));
  147.     code = pcx_write_page(pdev, file, &header, 0);
  148.     if ( code >= 0 )
  149.     {    /* Write out the palette. */
  150.         fputc(0x0c, file);
  151.         code = pc_write_palette((gx_device *)pdev, 256, file);
  152.     }
  153.     return code;
  154. }
  155.  
  156. /* Write a monochrome PCX page. */
  157. private int
  158. pcxmono_print_page(gx_device_printer *pdev, FILE *file)
  159. {    pcx_header header;
  160.     header.version = 2;
  161.     header.bpp = 1;
  162.     header.nplanes = 1;
  163.     /* Clear the EGA palette */
  164.     memset((byte *)header.palette, 0, sizeof(header.palette));
  165.     return pcx_write_page(pdev, file, &header, 0);
  166. }
  167.  
  168. /* Write out a page in PCX format. */
  169. /* This routine is used for all three formats (monochrome, planar */
  170. /* "8-bit" actually 4-bit color, and chunky 8-bit color.) */
  171. private int
  172. pcx_write_page(gx_device_printer *pdev, FILE *file, pcx_header _ss *phdr,
  173.   int planar)
  174. {    int raster = gdev_prn_raster(pdev);
  175.     int height = pdev->height;
  176.     int depth = pdev->color_info.depth;
  177.     uint rsize = (pdev->width + 7) >> 3;
  178.     byte *row = (byte *)gs_malloc(raster + rsize, 1, "pcx file buffer");
  179.     byte *end = row + raster;
  180.     byte *plane = end;
  181.     int y;
  182.     int code = 0;            /* return code */
  183.     if ( row == 0 )            /* can't allocate row buffer */
  184.         return_error(gs_error_VMerror);
  185.  
  186.     /* setup the header struct */
  187.  
  188.     phdr->manuf = 10;
  189.     /* version and bpp were set by the caller */
  190.     phdr->encoding = 1;    /* 1 for rle 8-bit encoding */
  191.     phdr->x1 = 0;
  192.     phdr->y1 = 0;
  193.     assign_ushort(phdr->x2, pdev->width-1);
  194.     assign_ushort(phdr->y2, height-1);
  195.     assign_ushort(phdr->hres, (int)pdev->x_pixels_per_inch);
  196.     assign_ushort(phdr->vres, (int)pdev->y_pixels_per_inch);
  197.     phdr->vmode = 0;
  198.     /* nplanes was set by the caller */
  199.     assign_ushort(phdr->bpl, (planar && depth > 1 ? rsize : raster));
  200.     assign_ushort(phdr->palinfo, (gx_device_has_color(pdev) ? 1 : 2));
  201.  
  202.     /* Write the header. */
  203.  
  204.     if ( fwrite((const char *)phdr, 1, 128, file) < 128 )
  205.        {    code = gs_error_ioerror;
  206.         goto pcx_done;
  207.        }
  208.  
  209.     /* Dump the contents of the image. */
  210.     for ( y = 0; y < height; y++ )
  211.        {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  212.         switch ( depth )
  213.            {
  214.         case 1:
  215.            {    /* PCX assumes 0=black, 1=white. */
  216.             register byte *bp;
  217.             for ( bp = row; bp < end; bp++ )
  218.                 *bp = ~*bp;
  219.             pcx_write_rle(row, end, file);
  220.            }
  221.             break;
  222.         case 8:
  223.            {    register int shift;
  224.  
  225.             if ( !gx_device_has_color(pdev) )
  226.                {    /* Can't map gray scale */
  227.                 code = gs_error_undefinedresult;
  228.                 goto pcx_done;
  229.                }
  230.  
  231.             if ( !planar )
  232.                {    /* Just write the bits */
  233.                 pcx_write_rle(row, end, file);
  234.                 break;
  235.                }
  236.  
  237.             for ( shift = 0; shift < 4; shift++ )
  238.             {    register byte *from, *to;
  239.                 register int bmask = 1 << shift;
  240.                 for ( from = row, to = plane;
  241.                       from < end; from += 8
  242.                     )
  243.                 {    *to++ =
  244.                       ((((uint)from[0] & bmask) << 7) +
  245.                        (((uint)from[1] & bmask) << 6) +
  246.                        (((uint)from[2] & bmask) << 5) +
  247.                        (((uint)from[3] & bmask) << 4) +
  248.                        (((uint)from[4] & bmask) << 3) +
  249.                        (((uint)from[5] & bmask) << 2) +
  250.                        (((uint)from[6] & bmask) << 1) +
  251.                        (((uint)from[7] & bmask)))
  252.                       >> shift;
  253.                 }
  254.                 pcx_write_rle(plane, to, file);
  255.             }
  256.            }
  257.             break;
  258.  
  259.         default:
  260.               code = gs_error_undefinedresult;
  261.               goto pcx_done;
  262.  
  263.            }
  264.        }
  265.  
  266. pcx_done:
  267.     gs_free((char *)row, raster + rsize, 1, "pcx file buffer");
  268.  
  269.     return code;
  270. }
  271.  
  272. /* ------ Internal routines ------ */
  273.  
  274. /* Write one line in PCX run-length-encoded format. */
  275. private void
  276. pcx_write_rle(const byte *from, const byte *end, FILE *file)
  277. {    while ( from < end )
  278.     {    byte data = *from++;
  279.         int count = 1;
  280.         while ( (from < end) && (*from == data) )
  281.             count++, from++;
  282.         while ( count > 63 )
  283.         {    putc(0xff, file);
  284.             putc(data, file);
  285.             count -= 63;
  286.         }
  287.         if ( count != 1 || data >= 0xc0 )
  288.         {    putc(count | 0xc0, file);
  289.         }
  290.         putc(data, file);
  291.     }
  292. }
  293.